package com.demo.redis.configuration;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

@Configuration
@Import(RedisPoolProperties.class)
public class SentinelRedisConfig {

    @Bean
    public JedisPoolConfig jedisPoolConfig(RedisPoolProperties redisPoolProperties) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(redisPoolProperties.getMaxTotal());
        jedisPoolConfig.setMaxIdle(redisPoolProperties.getMaxIdle());
        jedisPoolConfig.setMinIdle(redisPoolProperties.getMinIdle());
        jedisPoolConfig.setTestOnBorrow(redisPoolProperties.getTestOnBorrow());
        jedisPoolConfig.setTestOnReturn(redisPoolProperties.getTestOnReturn());
        jedisPoolConfig.setFairness(redisPoolProperties.getFairness());
        jedisPoolConfig.setMaxWaitMillis(redisPoolProperties.getMaxWaitMillis());
        jedisPoolConfig.setBlockWhenExhausted(redisPoolProperties.getBlockWhenExhausted());
        return jedisPoolConfig;
    }

    @Bean
    public JedisSentinelPool jedisSentinelPool(JedisPoolConfig jedisPoolConfig, RedisPoolProperties redisProperties) {
        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(redisProperties.getSentinelMaster(), redisProperties.getSentinels(), jedisPoolConfig, redisProperties.getTimeout());
        //JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(redisProperties.getSentinelMaster(), redisProperties.getSentinels(), jedisPoolConfig, redisProperties.getTimeout(), redisProperties.getPassword(), redisProperties.getDatabase());
        return jedisSentinelPool;
    }

    @Bean
    public RedisTemplate redisTemplate(JedisPoolConfig jedisPoolConfig, RedisPoolProperties redisProperties) {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration(redisProperties.getSentinelMaster(), redisProperties.getSentinels());
        redisSentinelConfiguration.setDatabase(0);
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration, jedisPoolConfig);
        jedisConnectionFactory.afterPropertiesSet();
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
        return redisTemplate;
    }
}
